//
import {reqGetCode, reqRegister,reqLogin,reqGetUserInfo} from '@/api';

const state={
    code:'',
    token:'',
    userInfo:{}
};
const mutations={
    GETCODE(state,code){
        state.code=code;
    },
    USERLOGINTOKEN(state,token){
        state.token=token;
    },
    GETUSERINFO(state,userInfo){
        state.userInfo=userInfo;
    }
};
const actions={
    //获取验证码
    async getCode({commit},phone){
        let result=await reqGetCode(phone);
        if(result.code==200){
            commit('GETCODE',result.data);
            return 'Ok';
        }else{
            return Promise.reject(new Error('fail'));
        }
    },
    //注册用户
    async registerUser({commit},data){
        let result= await reqRegister(data);
        if(result.code==200){
            console.log(result);
            return 'ok';
        }else{
            return Promise.reject(new Error('fail'));
        }
    },
    //登录
    async login({commit},data){
        let result= await reqLogin(data);
        console.log(result);
        if(result.code==200){
            commit('USERLOGINTOKEN',result.data.token);
            localStorage.setItem('TOKEN',result.data.token);
            return "ok";
        }else if(result.code==207){
            return Promise.reject(new Error('账号不正确'));
        }else if(result.code==202){
            return Promise.reject(new Error('参数不正确'));
        }else{
            return Promise.reject(new Error('fail'));
        }
    },
    //获取用户信息
    async getUserInfo({commit}){
        let result=await reqGetUserInfo();
        if(result.code==200){
            console.log(result);
            commit('GETUSERINFO',result.data);
        }
        
    }
};
const getters={};

export default {
    state,
    mutations,
    actions,
    getters
}